package com.luchao;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

public class htmlparser {

    public static void main(String[] args) throws IOException {

        String url = "https://www.youneed.win/free-ss";
        String rawHTML = null;
        try {
            rawHTML = getHTMLContent(url);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //将当前页面转换成Jsoup的Document对象
        Document doc = Jsoup.parse(rawHTML);

        //集合
        Elements infos = doc.select("td[align=center]");

        //账号密码集合
        List list = new ArrayList();
        ssinfo s = null;
        for (int i = 0; i < infos.size(); i++) {
//            Element element = infos.get(i);
//            System.out.println("-" + element.text());
            if(i==0 || i%6==0){
                s = new ssinfo();
            }
            s.setIp(infos.get(i+0).text());
            s.setPort(infos.get(i+1).text());
            s.setPassword(infos.get(i+2).text());
            s.setEnctype(infos.get(i+3).text());
            s.setUpdatedate(infos.get(i+4).text());
            s.setCountry(infos.get(i+5).text());
            list.add(s);
            i=i+5;
            
        }
        System.out.println("--"+list.size());

        for (int j = 0; j < list.size(); j++) {
            ssinfo ss = (ssinfo) list.get(j);
            String str = ss.getEnctype()+":"+ss.getPassword()+"@"+ss.getIp()+":"+ss.getPort();
            System.out.println("拼接后："+str);
            String s1 = "ss://"+Base64.getEncoder().encodeToString(str.getBytes(StandardCharsets.UTF_8));
            System.out.println("加密后："+s1);
        }


    }

    //根据url地址获取对应页面的HTML内容，我们将上一节中的内容打包成了一个方法，方便调用
    private static String getHTMLContent(String url) throws IOException {

        //建立一个新的请求客户端
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //使用HttpGet方式请求网址
        HttpGet httpGet = new HttpGet(url);
        //获取网址的返回结果
        CloseableHttpResponse response = httpClient.execute(httpGet);
        //获取返回结果中的实体
        HttpEntity entity = response.getEntity();
        String content = EntityUtils.toString(entity);
        //关闭HttpEntity流
        EntityUtils.consume(entity);
        return content;

    }

}
class ssinfo{
    private String ip;
    private String port;
    private String password;
    private String enctype;//加密方式
    private String updatedate;
    private String country;

    public String getEnctype() {
        return enctype;
    }

    public void setEnctype(String enctype) {
        this.enctype = enctype;
    }

    public String getUpdatedate() {
        return updatedate;
    }

    public void setUpdatedate(String updatedate) {
        this.updatedate = updatedate;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}